<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="formData.userId" placeholder="请输入用户ID" />
      </el-form-item>
      <el-form-item label="部门ID" prop="deptId">
        <el-input v-model="formData.deptId" placeholder="请输入部门ID" />
      </el-form-item>
      <el-form-item label="校验方式（0.关闭校验 1. 二要素校验 2. 三要素校验）" prop="validMethod">
        <el-radio-group v-model="formData.validMethod">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="查询姓名" prop="queryName">
        <el-input v-model="formData.queryName" placeholder="请输入查询姓名" />
      </el-form-item>
      <el-form-item label="查询证件号" prop="queryIdCard">
        <el-input v-model="formData.queryIdCard" placeholder="请输入查询证件号" />
      </el-form-item>
      <el-form-item label="查询手机号" prop="queryMobile">
        <el-input v-model="formData.queryMobile" placeholder="请输入查询手机号" />
      </el-form-item>
      <el-form-item label="查询类型（多个）" prop="queryType">
        <el-select v-model="formData.queryType" placeholder="请选择查询类型（多个）">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="查询时间" prop="queryTime">
        <el-date-picker
          v-model="formData.queryTime"
          type="date"
          value-format="x"
          placeholder="选择查询时间"
        />
      </el-form-item>
      <el-form-item label="验证结果（0未校验 1校验一致 2校验不一致）" prop="vaildResult">
        <el-radio-group v-model="formData.vaildResult">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="生日" prop="validBirthday">
        <el-input v-model="formData.validBirthday" placeholder="请输入生日" />
      </el-form-item>
      <el-form-item label="地址" prop="validAddress">
        <el-input v-model="formData.validAddress" placeholder="请输入地址" />
      </el-form-item>
      <el-form-item label="城市类型（一-五线城市）" prop="validAddressType">
        <el-select v-model="formData.validAddressType" placeholder="请选择城市类型（一-五线城市）">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="星座" prop="validConstellation">
        <el-input v-model="formData.validConstellation" placeholder="请输入星座" />
      </el-form-item>
      <el-form-item label="性别" prop="validGender">
        <el-input v-model="formData.validGender" placeholder="请输入性别" />
      </el-form-item>
      <el-form-item label="年龄" prop="validAge">
        <el-input v-model="formData.validAge" placeholder="请输入年龄" />
      </el-form-item>
      <el-form-item label="风险类型（1安全客户；2撸子；3疑似撸）" prop="riskType">
        <el-radio-group v-model="formData.riskType">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="风险记录（当客户撸子/疑似撸的时候，记录真假财务里的ID）" prop="riskTypeFinanceIds">
        <el-input v-model="formData.riskTypeFinanceIds" placeholder="请输入风险记录（当客户撸子/疑似撸的时候，记录真假财务里的ID）" />
      </el-form-item>
      <el-form-item label="是否是智能审核（0否 1是）" prop="isAudit">
        <el-radio-group v-model="formData.isAudit">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="智能审核-内容" prop="autidContent">
        <el-input v-model="formData.autidContent" type="textarea" placeholder="请输入智能审核-内容" />
      </el-form-item>
      <el-form-item label="智能审核-是否加入黑名单（0否 1是）" prop="auditIsBlacklist">
        <el-radio-group v-model="formData.auditIsBlacklist">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DataQueryApi, DataQuery } from '@/api/business/dataquery'

/** 数据查询 表单 */
defineOptions({ name: 'DataQueryForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  userId: undefined,
  deptId: undefined,
  validMethod: undefined,
  queryName: undefined,
  queryIdCard: undefined,
  queryMobile: undefined,
  queryType: undefined,
  queryTime: undefined,
  vaildResult: undefined,
  validBirthday: undefined,
  validAddress: undefined,
  validAddressType: undefined,
  validConstellation: undefined,
  validGender: undefined,
  validAge: undefined,
  riskType: undefined,
  riskTypeFinanceIds: undefined,
  isAudit: undefined,
  autidContent: undefined,
  auditIsBlacklist: undefined,
})
const formRules = reactive({
  userId: [{ required: true, message: '用户ID不能为空', trigger: 'blur' }],
  deptId: [{ required: true, message: '部门ID不能为空', trigger: 'blur' }],
  validMethod: [{ required: true, message: '校验方式（0.关闭校验 1. 二要素校验 2. 三要素校验）不能为空', trigger: 'blur' }],
  queryName: [{ required: true, message: '查询姓名不能为空', trigger: 'blur' }],
  queryIdCard: [{ required: true, message: '查询证件号不能为空', trigger: 'blur' }],
  queryType: [{ required: true, message: '查询类型（多个）不能为空', trigger: 'change' }],
  queryTime: [{ required: true, message: '查询时间不能为空', trigger: 'blur' }],
  riskType: [{ required: true, message: '风险类型（1安全客户；2撸子；3疑似撸）不能为空', trigger: 'blur' }],
  riskTypeFinanceIds: [{ required: true, message: '风险记录（当客户撸子/疑似撸的时候，记录真假财务里的ID）不能为空', trigger: 'blur' }],
  isAudit: [{ required: true, message: '是否是智能审核（0否 1是）不能为空', trigger: 'blur' }],
  autidContent: [{ required: true, message: '智能审核-内容不能为空', trigger: 'blur' }],
  auditIsBlacklist: [{ required: true, message: '智能审核-是否加入黑名单（0否 1是）不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await DataQueryApi.getDataQuery(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as DataQuery
    if (formType.value === 'create') {
      await DataQueryApi.createDataQuery(data)
      message.success(t('common.createSuccess'))
    } else {
      await DataQueryApi.updateDataQuery(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    userId: undefined,
    deptId: undefined,
    validMethod: undefined,
    queryName: undefined,
    queryIdCard: undefined,
    queryMobile: undefined,
    queryType: undefined,
    queryTime: undefined,
    vaildResult: undefined,
    validBirthday: undefined,
    validAddress: undefined,
    validAddressType: undefined,
    validConstellation: undefined,
    validGender: undefined,
    validAge: undefined,
    riskType: undefined,
    riskTypeFinanceIds: undefined,
    isAudit: undefined,
    autidContent: undefined,
    auditIsBlacklist: undefined,
  }
  formRef.value?.resetFields()
}
</script>